<?php
class SubfolderAccessManager_Util_Email {

    public function sendNewAccountEmail($account)
    {
        $userToAddress = $account['email_address'];
        $subject = 'New Account Information';

        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();

        $msg = file_get_contents('templates/user_added_tmplt.html');
        $msg = str_replace('{{username}}', $account['username'], $msg);
        $msg = str_replace('{{password}}', $account['password'], $msg);
        $msg = str_replace('{{role}}', $account['role'], $msg);
        $msg = str_replace('{{baseUrl}}', SubfolderAccessManager_Util_Url::_siteURL().$baseUrl, $msg);

        $adminEmail = $this->_getAdminEmail();

        $mail = new Zend_Mail();
        $mail->setFrom($adminEmail);
        $mail->addTo($userToAddress);
        $mail->setSubject($subject);
        $mail->setBodyHtml($msg);
        $mail->send();
    }

    public function sendUpdatedAccountEmail($account)
    {
        $userToAddress = $account['email_address'];
        $subject = 'Account Profile Update Information';

        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();

        $msg = file_get_contents('templates/user_updated_tmplt.html');
        $msg = str_replace('{{email_address}}', $account['email_address'], $msg);
        $msg = str_replace('{{first_name}}', $account['first_name'], $msg);
        $msg = str_replace('{{last_name}}', $account['last_name'], $msg);
        $msg = str_replace('{{role}}', $account['role'], $msg);
        $msg = str_replace('{{baseUrl}}', SubfolderAccessManager_Util_Url::_siteURL().$baseUrl, $msg);

        $adminEmail = $this->_getAdminEmail();

        $mail = new Zend_Mail();
        $mail->setFrom($adminEmail);
        $mail->addTo($userToAddress);
        $mail->setSubject($subject);
        $mail->setBodyHtml($msg);
        $mail->send();
    }

    public function sendEmailChangedNotification($oldEmail, $newEmail)
    {
        $subject = 'Email Address Change Confirmation';

        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();

        $msg = file_get_contents('templates/user_email_changed_tmplt.html');
        $msg = str_replace('{{old_email}}', $oldEmail, $msg);
        $msg = str_replace('{{new_email}}', $newEmail, $msg);
        $msg = str_replace('{{baseUrl}}', SubfolderAccessManager_Util_Url::_siteURL().$baseUrl, $msg);

        $adminEmail = $this->_getAdminEmail();

        $mail = new Zend_Mail();
        $mail->setFrom($adminEmail);
        $mail->addTo(array($oldEmail, $newEmail));
        $mail->setSubject($subject);
        $mail->setBodyHtml($msg);
        $mail->send();
    }

    public function sendPasswordChangedNotification($account, $newPass)
    {
        $userToAddress = $account['email_address'];
        $subject = 'Password Change Confirmation';

        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();

        $msg = file_get_contents('templates/user_password_changed_tmplt.html');
        $msg = str_replace('{{username}}', $account['username'], $msg);
        $msg = str_replace('{{password}}', $newPass, $msg);
        $msg = str_replace('{{baseUrl}}', SubfolderAccessManager_Util_Url::_siteURL().$baseUrl, $msg);

        $adminEmail = $this->_getAdminEmail();

        $mail = new Zend_Mail();
        $mail->setFrom($adminEmail);
        $mail->addTo($userToAddress);
        $mail->setSubject($subject);
        $mail->setBodyHtml($msg);
        $mail->send();
    }

    public function sendSubfolderAccessNotification($rootDir, $masterPath, $subfolderName, $isEnabled)
    {
        $template = '';
        $subject = '';

        if ($isEnabled) {
            $template = 'templates/subfolder_access_enabled_tmplt.html';
            $subject = 'Subfolder Access Enabled';
        } else {
            $template = 'templates/subfolder_access_disabled_tmplt.html';
            $subject = 'Subfolder Access Disabled';
        }

        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        $subDir = str_replace($rootDir, '', $masterPath);

        $msg = file_get_contents($template);
        $msg = str_replace('{{subfolder}}', $masterPath . $subfolderName, $msg);
        $msg = str_replace('{{subfolderLink}}', SubfolderAccessManager_Util_Url::_siteURL(). '/' . $subDir . $subfolderName, $msg);

        $adminEmail = $this->_getAdminEmail();

        $mail = new Zend_Mail();
        $mail->setFrom($adminEmail);
        $mail->addTo($this->_getSubfolderNotifReceipients());
        $mail->setSubject($subject);
        $mail->setBodyHtml($msg);
        $mail->send();
    }

    protected function _getAdminEmail()
    {
        $config = Zend_Registry::get('config');
        return $config->admin->email;
    }

    protected function _getCurrentUserEmail()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            return $auth->getIdentity()->email_address;
        } else {
            return null;
        }
    }

    protected function _getSubfolderNotifReceipients() {
        $config = Zend_Registry::get('config');

        $receipients = explode(',', $config->subfolder->notification->receipients);

        return $receipients;
    }
}